<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/3/26
 * Time: 13:51
 */
namespace Modules\Service\Logic;
use App\Models\Advs;
use App\Models\Attachments;
use App\Models\Goods;
use App\Models\GoodsClass;
use App\Models\GoodsSku;
use Illuminate\Support\Facades\Log;

class GoodsLogic extends BaseLogic{

    const PATH = "http://api.gushitech.com/v2/";
    //商品类型列表
    public static function goodsClass(){
        try{
            //查询广告
            $shopAdvs = Advs::advsImage("shop");
            //查询商品类型
            $classData = GoodsClass::where("pid",0)->where("status",1)->with("image")->orderBy('sort','asc')->get();
            $data = [];
            if ($classData->count()>0){
                foreach ($classData as $item){
                    $typeData = GoodsClass::where("pid",$item->id)->select("id","class_name")->get();
                    $data[] = [
                        "id" => $item->id,
                        "class_name" => $item->class_name,
                        "image" => $item['image']['file_url'],
                        "url" => $item->url,
                        "child"=> $typeData
                    ];
                }
            }
            //热门好物
            $hotGoods = Goods::where("hot",1)->where('is_onsale',1)->limit(4)->select('id', 'goods_name','header', 'image', 'sell_price', 'original_price', 'sales_volume', 'detail')->get();
            $goods = [];
            if ($hotGoods->count()>0){
                foreach ($hotGoods as $item){
                    $imageUrl = (Attachments::find($item->header))->file_url;
                    $goods[] = [
                        "id" => $item->id,
                        "goods_name" => $item->goods_name,
                        "image" => $imageUrl,
                        "sell_price" => $item->sell_price,
                        "original_pirce" => $item->original_price,
                        "sales_volume" => $item->sales_volume,
                        "store_num" => $item->store_num,
                        "url" => self::PATH.'api/shop/detail/'.$item->id
                    ];
                }
            }
            $array = ["menu"=>$data,"advs"=>$shopAdvs,"hot_goods"=>$goods];
            return success($array);
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

    //精选商品
    public static function goodsSelectedOrHot($page,$limit){
        try{
                //查询精选
            $selectCount = Goods::where("selected",1)->where('is_onsale',1)->count();
            $selectData = Goods::where("selected", 1)->where('is_onsale',1)->forPage($page,$limit)->select('id', 'goods_name','header', 'image', 'sell_price', 'original_price', 'sales_volume', 'detail')->get();
            $goods = [];
            if ($selectData->count()>0){
                foreach ($selectData as $item){
                    $imageUrl = (Attachments::find($item->header))->file_url;
                    $goods[] = [
                        "id" => $item->id,
                        "goods_name" => $item->goods_name,
                        "image" => $imageUrl,
                        "sell_price" => $item->sell_price,
                        "original_pirce" => $item->original_price,
                        "sales_volume" => $item->sales_volume,
                        "store_num" => $item->store_num,
                        "url" => self::PATH.'api/shop/detail/'.$item->id
                    ];
                }
            }
            return success(["count"=>$selectCount,"data"=>$goods]);
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

    //商品列表
    public static function goodsClassById($classId,$typeId,$sort,$desc,$page,$limit){
        try{
            //定义二级分类ID
            $secondId = 0;
            //判断二级分类Id是否存在
            if ($classId && !$typeId){
                $classData = GoodsClass::where("pid",$classId)->orderBy('id','asc')->first();
                if ($classData){
                    $secondId = $classData->id;
                }
            }else{
                $secondId = $typeId;
            }
            $goodsCount = Goods::where('class_id',$classId)->where('type_id',$secondId)->where('is_onsale',1)->count();
            $goodsData = Goods::where('class_id',$classId)
                ->where("type_id",$secondId)
                ->where('is_onsale',1)
                ->forPage($page,$limit)
                ->orderBy($sort,$desc)
                ->select('id', 'goods_name', 'header', 'image', 'sell_price', 'original_price', 'sales_volume')
                ->get();
            $goods = [];
            if ($goodsData->count()>0){
                foreach ($goodsData as $item){
                    $imageUrl = (Attachments::find($item->header))->file_url;

                    $goods[] = [
                        "id" => $item->id,
                        "goods_name" => $item->goods_name,
                        "image" => $imageUrl,
                        "sell_price" => $item->sell_price,
                        "original_pirce" => $item->original_price,
                        "sales_volume" => $item->sales_volume,
                        "store_num" => $item->store_num,
                        "url" => self::PATH.'api/shop/detail/'.$item->id
                    ];
                }
            }
            return success(['count'=>$goodsCount,'data'=>$goods]);
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

    //商品详情
    public static function goodsDetail($id){
        try{
            //查询商品
            $goodsData = Goods::find($id);
            if (!$goodsData)return fail(1021,"找不到此商品");
            $image = explode(',',$goodsData->image);
            $detail = explode(',',$goodsData->detail);
            $imageData = [];
            $detailData = [];
            //商品
            foreach ($image as $k=>$v){
                $imageData[] = (Attachments::find($v))->file_url;
            }
            //详情
            foreach ($detail as $k=>$v){
                $detailData[] = (Attachments::find($v))->file_url;
            }
            //查询规格
            $skuData = GoodsSku::where("goods_id",$id)->select("id","sku_name","sku_price")->get();
            $goods = [
                "id" => $goodsData->id,
                "goods_name" => $goodsData->goods_name,
                "image" => $imageData[0],
                "banner" => $imageData,
                "sell_price" => $goodsData->sell_price,
                "original_pirce" => $goodsData->original_price,
                "sales_volume" => $goodsData->sales_volume,
                "store_num" => $goodsData->store_num,
                "detail" => $detailData,
                "sku"=>$skuData
            ];
            return success($goods);
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

}